package com.koron.order.overhaul.service.impl;

import cn.hutool.core.lang.Assert;
import com.koron.common.core.util.CodeTools;
import com.koron.order.overhaul.bean.entity.OverhaulOrderItemParamsBean;
import com.koron.order.overhaul.bean.query.OverhaulOrderItemParamsQuery;
import com.koron.order.overhaul.mapper.OverhaulOrderItemParamsMapper;
import com.koron.order.overhaul.service.OverhaulOrderItemParamsService;
import com.koron.order.overhaul.utils.CommonUtils;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;

/**
 * (OverhaulOrderItemParamsBean)表服务实现类
 * @author twcao
 * @since 2021-09-29 09:49:29
 */
@Service
public class OverhaulItemParamsServiceImpl implements OverhaulOrderItemParamsService {

    @Override
    public OverhaulOrderItemParamsBean queryById(SessionFactory factory, String id) {
        OverhaulOrderItemParamsMapper mapper = factory.getMapper(OverhaulOrderItemParamsMapper.class);
        OverhaulOrderItemParamsBean bean = mapper.queryById(id);
        return bean;
    }

    @Override
    public int saveOrUpdate(SessionFactory factory, List<OverhaulOrderItemParamsBean> beans) {
        if(CollectionUtils.isEmpty(beans)) {
            return 0;
        }
        OverhaulOrderItemParamsMapper mapper = factory.getMapper(OverhaulOrderItemParamsMapper.class);
        CommonUtils.update(beans);
        beans.stream().forEach(row -> row.setId(StringUtils.isBlank(row.getId()) ? CodeTools.getCode32() : row.getId()));
        return mapper.saveOrUpdate(beans);
    }

    @Override
    public int saveOrUpdate(SessionFactory factory, OverhaulOrderItemParamsBean wOverhaulItemParam) {
        Assert.notNull(wOverhaulItemParam, "参数不能为空");
        OverhaulOrderItemParamsMapper mapper = factory.getMapper(OverhaulOrderItemParamsMapper.class);
        CommonUtils.update(wOverhaulItemParam);
        wOverhaulItemParam.setId(StringUtils.isBlank(wOverhaulItemParam.getId()) ? CodeTools.getCode32() : wOverhaulItemParam.getId());
        return mapper.saveOrUpdate(Arrays.asList(wOverhaulItemParam));
    }

    @Override
    public int deleteByOrderId(SessionFactory factory, String orderId) {
        OverhaulOrderItemParamsMapper mapper = factory.getMapper(OverhaulOrderItemParamsMapper.class);
        return mapper.deleteByOrderId(orderId);
    }

    @Override
    public int deleteByOrderIdAndItemId(SessionFactory factory, String orderId,String itemId) {
        OverhaulOrderItemParamsMapper mapper = factory.getMapper(OverhaulOrderItemParamsMapper.class);
        return mapper.deleteByOrderIdAndItemId(orderId,itemId);
    }

    @Override
    public int hiddenByOrderId(SessionFactory factory, String orderId) {
        OverhaulOrderItemParamsMapper mapper = factory.getMapper(OverhaulOrderItemParamsMapper.class);
        return mapper.hiddenByOrderId(orderId);
    }

    @Override
    public List<OverhaulOrderItemParamsBean> queryList(SessionFactory factory, OverhaulOrderItemParamsQuery query) {
        OverhaulOrderItemParamsMapper mapper = factory.getMapper(OverhaulOrderItemParamsMapper.class);
        return mapper.queryList(query);
    }

}
